package com.onlinevideo.dao.impl;

import com.onlinevideo.dao.BaseDao;
import com.onlinevideo.dao.MessageDao;
import com.onlinevideo.entity.Message;

import java.util.List;

public class MessageDaoImpl extends BaseDao implements MessageDao {

    @Override
    public int insert(Message message) {
        String sql = "INSERT INTO `T_message` (`user_id`,`context`,`has_read`,`time`) VALUES (?,?,?,?)";
        return super.update(sql,message.getUserId(),message.getContext(),message.getHasRead(),message.getTime());
    }

    @Override
    public int delete(Long id) {
        String sql = "DELETE FROM `T_message` WHERE `id`=?";
        return super.update(sql, id);
    }

    @Override
    public int update(Message message) {
        String sql = "UPDATE `T_message` SET `user_id`=?,`context`=?,`has_read`=?,`time`=? WHERE `id`=?";
        return super.update(sql,message.getUserId(),message.getContext(),message.getHasRead(),message.getTime(),message.getId());
    }

    @Override
    public Message queryById(Long id) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`context`,`has_read` AS `hasRead`,`time` FROM `T_message` WHERE `id`=?";
        return super.queryForOne(Message.class, sql, id);
    }

    @Override
    public List<Message> queryAll() {
        String sql = "SELECT `id`,`user_id` AS `userId`,`context`,`has_read` AS `hasRead`,`time` FROM `T_message`";
        return super.queryForList(Message.class, sql);
    }

    @Override
    public List<Message> queryByUserId(Long userId, Integer haveRead) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`context`,`has_read` AS `hasRead`,`time` FROM `T_message` WHERE `user_id`=? AND `has_read`=? ORDER BY `time` DESC";
        return super.queryForList(Message.class,sql,userId,haveRead);

    }

    @Override
    public int deleteByUserRead(Long userId) {
        String sql = "DELETE FROM `T_message` WHERE `user_id`=? AND `has_read`=1";
        return super.update(sql, userId);
    }
}
